package com.immoc.web.servlet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.immoc.domain.User;
import com.immoc.service.UserService;
import com.immoc.service.impl.UserServiceImpl;
import com.immoc.utils.UploadUtils;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 接受数据
        // 1.创建磁盘文件项工厂
        Map<String, String> map = new HashMap<>();
        DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
        // 2.创建核心解析类
        ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory);
        // 3.解析请求对象,将请求分成几个部分(FileItem)
        try {
            List<FileItem> list = fileUpload.parseRequest(request);
            // 4.遍历集合获得每个部分的对象
            for (FileItem fileItem : list) {
                // 判断是普通项还是文件上传项
                if (fileItem.isFormField()) {
                    // 普通项
                    // 获得普通项的名称:
                    String name = fileItem.getFieldName();
                    // 获得普通项的值:
                    String value = fileItem.getString("UTF-8");
                    map.put(name, value);
                } else {
                    // 文件上传项
                    // 获得文件的名称:
                    String fileName = fileItem.getName();
                    String uufileName = UploadUtils.getUuidFileName(fileName);
                    // 获得文件的输入流:
                    InputStream is = fileItem.getInputStream();
                    // 需要将文件写入到服务器的某个路径即可:
                    String path = getServletContext().getRealPath("/upload");
                    System.out.println(path);
                    // 显示图片
                    // 创建输出流 与 输入流进行对接:
                    map.put("path", request.getContextPath() + "/upload/" + uufileName);
                    String url = path + "\\" + uufileName;
                    OutputStream os = new FileOutputStream(url);
                    int len = 0;
                    byte[] b = new byte[1024];
                    while ((len = is.read(b)) != -1) {
                        os.write(b, 0, len);
                    }
                    is.close();
                    os.close();
                }
            }
        } catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // 封装数据
        User user = new User();
        user.setUsername(map.get("username"));
        user.setPassword(map.get("password"));
        user.setPath(map.get("path"));
        System.out.println(user);
        UserService userService = new UserServiceImpl();
        List<User> userList = (List<User>) request.getServletContext().getAttribute("userList");
        userService.regist(userList, user);
        response.sendRedirect(request.getContextPath() + "/login.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
